package com.gw.quotes.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gw.quotes.R;
import com.gw.quotes.domain.Gift;

public class GiftAdapter extends BaseAdapter {

	private List<Gift> list;
	private Context context;
	private int resource;

	public GiftAdapter(Context context, int layoutId, List<Gift> list) {
		this.context = context;
		this.resource = layoutId;
		this.list = list;
	}

	@Override
	public int getCount() {
		return this.list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Gift gift = list.get(position);
		LinearLayout layout = null;
		if (convertView == null) {
			layout = (LinearLayout) LayoutInflater.from(this.context).inflate(
					resource, null);
		} else {
			layout = (LinearLayout) convertView;
		}
		TextView gift_text = (TextView) layout.findViewById(R.id.gift_text);
		String str = gift.getGift_name() + " X" + gift.getGift_num();
		int start = 0;
		for (int i = 0; i < str.length(); i++) {
			if ('X' == str.charAt(i)) {
				start = i;
			}
		}
		SpannableStringBuilder style = new SpannableStringBuilder(str);
		style.setSpan(new ForegroundColorSpan(Color.RED), start, str.length(),
				Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
		gift_text.setText(style);
		return layout;
	}
}
